00001 // Emacs Mode Line: -*- Mode:c++;-*- 00002 // ------------------------------------------------------------- 00003 00004 // ------------------------------------------------------------- 00005 // ------------------------------------------------------------- 00006 /** 00007 * @file linear_solver_interface.hpp 00008 * @author William A. Perkins 00009 * @date 2015-08-14 14:51:30 d3g096 00010 * 00011 * @brief 00012 * 00013 * 00014 */ 00015 // ------------------------------------------------------------- 00016 // ------------------------------------------------------------- 00017 // Created October 21, 2014 by William A. Perkins 00018 // Last Change: 2013-05-03 12:23:12 d3g096 00019 // ------------------------------------------------------------- 00020 00021 00022 #ifndef _linear_solver_interface_hpp_ 00023 #define _linear_solver_interface_hpp_ 00024 00025 #include "gridpack/math/matrix.hpp" 00026 00027 namespace gridpack { 00028 namespace math { 00029 00030 00031 // ------------------------------------------------------------- 00032 // class BaseLinearSolverInterface 00033 // ------------------------------------------------------------- 00034 template <typename T, typename I> 00035 class BaseLinearSolverInterface 00036 // : public ImplementationVisitable 00037 { 00038 public: 00039 00040 typedef MatrixT<T, I> MatrixType; 00041 typedef VectorT<T, I> VectorType; 00042 00043 /// Default constructor. 00044 BaseLinearSolverInterface(void) 00045 {} 00046 00047 /// Destructor 00048 ~BaseLinearSolverInterface(void) 00049 {} 00050 00051 /// Get the solution tolerance 00052 /** 00053 * 00054 * 00055 * 00056 * @return current solution tolerance 00057 */ 00058 double tolerance(void) const 00059 { 00060 return this->p_tolerance(); 00061 } 00062 00063 /// Set the solver tolerance 00064 /** 00065 * 00066 * 00067 * @param tol new solution tolerance 00068 */ 00069 void tolerance(const double& tol) 00070 { 00071 this->p_tolerance(tol); 00072 } 00073 00074 /// Get the maximum iterations 00075 /** 00076 * 00077 * 00078 * 00079 * @return current maximum number of solution iterations 00080 */ 00081 int maximumIterations(void) const 00082 { 00083 return this->p_maximumIterations(); 00084 } 00085 00086 00087 /// Set the maximum solution iterations 00088 /** 00089 * 00090 * 00091 * @param n new maximum number of iterations 00092 */ 00093 void maximumIterations(const int& n) 00094 { 00095 this->p_maximumIterations(n); 00096 } 00097 00098 /// Solve w/ the specified RHS, put result in specified vector 00099 /** 00100 * @e Collective. 00101 * 00102 * Solve a linear system of equations. When called, Vector @c x 00103 * should contain the initial solution estimate. The final solution 00104 * is returned in @c x. 00105 * 00106 * The \ref parallel::Communicator "communicator" @c x and @c b must 00107 * be the same and match that of the coefficient Matrix used for 00108 * construction. The length of both @c x and @c b must the number of 00109 * columns in the coeffienct Matrix used for constructor or passed 00110 * the last call to setMatrix(). If these conditions are not met, 00111 * an \ref Exception "exception" is thrown. 00112 * 00113 * @param b Vector containing right hand side of linear system 00114 * @param x solution Vector 00115 */ 00116 void solve(const VectorType& b, VectorType& x) const 00117 { 00118 this->p_solve(b, x); 00119 } 00120 00121 /// Solve again w/ the specified RHS, put result in specified vector 00122 /** 00123 * @e Collective. 00124 * 00125 * Solve a linear system of equations again with a different 00126 * RHS. This can only be called after ::solve() has been called once 00127 * and only if the coefficient matrix has not changed. Vector @c x 00128 * should contain the initial solution estimate. The final solution 00129 * is returned in @c x. 00130 * 00131 * The \ref parallel::Communicator "communicator" @c x and @c b must 00132 * be the same and match that of the coefficient Matrix used for 00133 * construction. The length of both @c x and @c b must the number of 00134 * columns in the coeffienct Matrix used for constructor or passed 00135 * the last call to setMatrix(). If these conditions are not met, 00136 * an \ref Exception "exception" is thrown. 00137 * 00138 * @param b Vector containing right hand side of linear system 00139 * @param x solution Vector 00140 */ 00141 void resolve(const VectorType& b, VectorType& x) const 00142 { 00143 this->p_resolve(b, x); 00144 } 00145 00146 /// Solve multiple systems w/ each column of the Matrix a single RHS 00147 /** 00148 * 00149 * 00150 * @param B RHS matrix -- each column is used as a RHS Vector 00151 * 00152 * @return @e dense solution Matrix -- each column is the solution for the corresponding column in @c B 00153 */ 00154 MatrixType *solve(const MatrixType& B) const 00155 { 00156 return this->p_solve(B); 00157 } 00158 00159 00160 protected: 00161 00162 /// Get the solution tolerance (specialized) 00163 virtual double p_tolerance(void) const = 0; 00164 00165 /// Set the solver tolerance (specialized) 00166 virtual void p_tolerance(const double& tol) = 0; 00167 00168 /// Get the maximum iterations (specialized) 00169 virtual int p_maximumIterations(void) const = 0; 00170 00171 /// Set the maximum solution iterations 00172 virtual void p_maximumIterations(const int& n) = 0; 00173 00174 /// Solve w/ the specified RHS, put result in specified vector 00175 /** 00176 * Can be called repeatedly with different @c b and @c x vectors 00177 * 00178 * @param b Vector containing right hand side of linear system 00179 * @param x Vector containing initial solution estimate; final 00180 * solution is put into this. 00181 */ 00182 virtual void p_solve(const VectorType& b, VectorType& x) const = 0; 00183 00184 /// Solve again w/ the specified RHS, put result in specified vector 00185 /** 00186 * This assumes that the coefficient matrix has not changed. Can be 00187 * called repeatedly with different @c b and @c x vectors 00188 * 00189 * @param b Vector containing right hand side of linear system 00190 * @param x Vector containing initial solution estimate; final 00191 * solution is put into this. 00192 */ 00193 virtual void p_resolve(const VectorType& b, VectorType& x) const = 0; 00194 00195 /// Solve multiple systems w/ each column of the Matrix a single RHS 00196 virtual MatrixType *p_solve(const MatrixType& B) const = 0; 00197 00198 }; 00199 00200 00201 00202 } // namespace math 00203 } // namespace gridpack 00204 00205 00206 #endif